using System;
using System.Reflection;
using System.Windows;
using System.Windows.Forms;
using Microsoft.SmallBasic.Library;
using SmallBasicFun.Tracking;

namespace SmallBasicFun
{
	/// <summary>
	/// 	This is the main window of your program.
	/// 	This is where the turtle, lines, shapes and images will appear.
	/// </summary>
	[SmallBasicType]
	public static class ProgramWindow
	{
		private static ITrackingEvent tracking = null;

		static ProgramWindow()
		{
			Show();
			//AttachTrackingEvent(new ScreenCapture());
			AttachTrackingEvent(new GoogleAnalyticsTrack());
		}
		private static void AttachTrackingEvent(ITrackingEvent screenCapture)
		{
			if (tracking == null)
			{
				tracking = screenCapture;
				GraphicsWindowHelper.AddClosingEventFirst(screenCapture.CaptureClosingEvent());
				Launcher.Start(2000, screenCapture.CaptureInactiveEvent());
			}
		}
		/// <summary>
		/// 	Invoke your subroutine each time the mouse is clicked
		/// </summary>
		/// <example>
		/// 	ProgramWindow.MouseClicked = Cheese
		/// 	Sub Cheese
		/// 	EndSub
		/// </example>
		public static event SmallBasicCallback MouseClicked
		{
			add { GraphicsWindow.MouseDown += value; }

			remove { GraphicsWindow.MouseDown -= value; }
		}

		/// <summary>
		/// 	Draws an image, resized to the current dimensions of the window
		/// </summary>
		/// <example>
		/// 	ProgramWindow.DrawBackgroundImage(Flickr.GetPictureOfMoment())
		/// </example>
		/// <param name="image"> The image to be drawn </param>
		public static void DrawBackgroundImage(Primitive image)
		{
			GraphicsWindow.DrawResizedImage(image, 0, 0, GraphicsWindow.Width, GraphicsWindow.Height);
		}

		/// <summary>
		/// 	Wipes the window clean.
		/// </summary>
		/// <example>
		/// 	ProgramWindow.Clear()
		/// </example>
		public static void Clear()
		{
			GraphicsWindow.Clear();
		}


		/// <summary>
		/// 	Draws some words on the Program Window using the settings from CurrentFont (size, etc...)
		/// </summary>
		/// <example>
		/// 	ProgramWindow.DrawText("Small Basic Rocks",0,0)
		/// </example>
		/// <param name="text"> The words you want to write </param>
		/// <param name="x"> The X coordinate of top left corner of the words </param>
		/// <param name="y"> The Y coordinate of top left corner of the words </param>
		public static void DrawText(Primitive text, Primitive x, Primitive y)
		{
			GraphicsWindow.DrawText(x, y, text);
		}

		/// <summary>
		/// 	Gets the location of the Mouse relative to the Top Left of the Program Window.
		/// </summary>
		/// <example>
		/// 	x = ProgramWindow.GetMouseX()
		/// </example>
		/// <returns> The x coordinate </returns>
		public static Primitive GetMouseX()
		{
			return GetMouse().X;
		}

		private static Point GetMouse()
		{
			GraphicsWindow.Show();
			return GraphicsWindowHelper.InvokeWithReturn(() =>
				{
					var p = new Point(Cursor.Position.X, Cursor.Position.Y);
					return
						GraphicsWindowHelper.GetCanvas().PointFromScreen(p);
				});
		}

		/// <summary>
		/// 	Gets the location of the Mouse relative to the Top Left of the Program Window.
		/// </summary>
		/// <example>
		/// 	y = ProgramWindow.GetMouseY()
		/// </example>
		/// <returns> The y coordinate </returns>
		public static Primitive GetMouseY()
		{
			return GetMouse().Y;
		}

		/// <summary>
		/// 	Changes the background of the Program Window to the color of your choice.
		/// </summary>
		/// <example>
		/// 	ProgramWindow.SetBackgroundColor("Red")
		/// </example>
		/// <param name="color"> The color you want the background to be. </param>
		public static void SetBackgroundColor(Primitive color)
		{
			GraphicsWindow.BackgroundColor = color;
		}

		/// <summary>
		/// 	Retrieves the current color of the pixel at the (x,y) location.
		/// </summary>
		/// <example>
		/// 	color = ProgramWindow.GetPixelColor(48, 34)
		/// </example>
		/// <param name="x"> The X coordinate of the pixel </param>
		/// <param name="y"> The Y coordinate of the pixel </param>
		/// <returns> The color of the pixel </returns>
		public static Primitive GetPixelColor(Primitive x, Primitive y)
		{
			return GraphicsWindow.GetPixel(x, y);
		}

		/// <summary>
		/// 	Gets the current width of the Window in pixels
		/// </summary>
		/// <example>
		/// 	w = ProgramWindow.GetWindowWidth()
		/// </example>
		/// <returns> width in pixels </returns>
		public static Primitive GetWindowWidth()
		{
			return GraphicsWindow.Width;
		}

		/// <summary>
		/// 	Gets the current height of the Window in pixels
		/// </summary>
		/// <example>
		/// 	w = ProgramWindow.GetWindowHeight()
		/// </example>
		/// <returns> height in pixels </returns>
		public static Primitive GetWindowHeight()
		{
			return GraphicsWindow.Height;
		}

		public static Window GetWindow()
		{
			BindingFlags flags = BindingFlags.Static | BindingFlags.NonPublic;
			Type t = typeof (GraphicsWindow);
			FieldInfo field = t.GetField("_window", flags);
			var window = (Window) field.GetValue(null);
			return window;
		}


		/// <summary>
		/// 	Opens a MessageBox and calls ShowMessage with the location of the FileName and ScreenShot location path
		/// </summary>
		public static void DisplayScreenShotLocation()
		{
			MessageBox.ShowMessage(ScreenCapture.ConcatentateDisplayScreenShotLocation());
		}


		


		/// <summary>
		/// 	Displays the Program Window.
		/// </summary>
		/// <example>
		/// 	ProgramWindow.Show()
		/// </example>
		public static void Show()
		{
			GraphicsWindow.Show();

			WindowManager.Add(GetWindow());
		}

		/// <summary>
		/// 	Changes the color the Text is written in.
		/// </summary>
		/// <example>
		/// 	ProgramWindow.SetTextColor(Colors.Tomato)
		/// </example>
		public static void SetTextColor(Primitive color)
		{
			GraphicsWindow.BrushColor = color;
		}

		public static void AbortTracking()
		{
			if (tracking != null)
			{
				tracking.Abort();
			}
		}
	}
}